import {createproduct} from '../../services/product/product';
import {message} from "antd";
import Constants from '../../utils/Constants'
const R = require('ramda');

export default {
    namespace: 'product',
    state: {},
    reducers: {},
    effects: {
        /**
         * 新增产品
         *
         * @param call
         * @param put
         * @param select
         */
        *createproduct({ payload }, { call, put, select }) {
            const res = yield call(createproduct, payload);
            if (res && res.data.resultCode === "ok") {
                message.info("产品添加成功！");
                if (payload.onSuccess) payload.onSuccess();
            } else {
                if (payload.onFail) payload.onFail();
            }
        },
    },
    subscriptions: {
        setup({ history, dispatch }) {
            return history.listen(({ pathname }) => {
                const menus = JSON.parse(sessionStorage.getItem(Constants.USER_MENU));
                if(menus && !menus.find(item=>item.includes(pathname)) && pathname != '/logout'){
                    document.location.href = "/notAuthorized"
                }
            });
        }
    }
};